/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package redezeitor;

import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Mauriverti
 */
public class Roteador extends Thread {

    private int id;                             // ID do roteador
    private int IP;                             // IP do roteador
    private int mask;                           // mascara do roteador
    Connector c;                                // Thread de coneccao

    Roteador() {
        id = 0;
        IP = 0;
        mask = 0;
        c = new Connector(id, 8000);
    }

    Roteador(int _id, int _IP, int _mask, int _port) {
        this.id = _id;
        this.IP = _IP;
        this.mask = _mask;
        this.c = new Connector(_id, _port);
    }

    @Override
    public void run() {

        Server s = new Server(this);
        s.start();

        while (true) {
            try {
                
                c.cliente();

            } catch (IOException ex) {
                Logger.getLogger(Roteador.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public int checkSubNetwork() {

        int subNetwork = 0;

        subNetwork = (this.IP & this.mask);

        System.out.println("SubRede: " + (subNetwork & 0xFF) + "." + ((subNetwork >> 8) & 0xFF) + "." + ((subNetwork >> 16) & 0xFF) + "." + ((subNetwork >> 24) & 0xFF));

        return subNetwork;
    }

    public int getRouterId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
        c.setIdRouter(id);
    }

    public int getIP() {
        //System.out.println("IP: " + (IP & 0xFF) + "." + ((IP >> 8) & 0xFF) + "." + ((IP >> 16) & 0xFF) + "." + ((IP >> 24) & 0xFF));
        return IP;
    }

    public String getStringIP() {
        String s = (IP & 0xFF) + "." + ((IP >> 8) & 0xFF) + "." + ((IP >> 16) & 0xFF) + "." + ((IP >> 24) & 0xFF);
        return s;
    }

    public String getStringMask() {
        String s = (mask & 0xFF) + "." + ((mask >> 8) & 0xFF) + "." + ((mask >> 16) & 0xFF) + "." + ((mask >> 24) & 0xFF);
        return s;
    }

    public String getStringSubNetwork() {
        String s = (getSubNetwork() & 0xFF) + "." + ((getSubNetwork() >> 8) & 0xFF) + "." + ((getSubNetwork() >> 16) & 0xFF) + "." + ((getSubNetwork() >> 24) & 0xFF);
        return s;
    }

    public void setIP(int IP1, int IP2, int IP3, int IP4) {
        this.IP = (((IP1 & 0xFF))
                | ((IP2 & 0xFF) << 8)
                | ((IP3 & 0xFF) << 16)
                | ((IP4 & 0xFF) << 24));
    }

    public void setIP(int _IP) {
        this.IP = _IP;
    }

    public int getSubNetwork() {

        return (this.IP & this.mask);
    }

    public void setMask(int mask1, int mask2, int mask3, int mask4) {
        this.mask = (((mask1 & 0xFF))
                | ((mask2 & 0xFF) << 8)
                | ((mask3 & 0xFF) << 16)
                | ((mask4 & 0xFF) << 24));
    }

    public void setMask(int _mask) {
        this.mask = _mask;
    }

    public void addLink(int idOtherRouter, int thisIP, int otherIP, int thisMask, int otherMask, int distance) {
        c.addLink(idOtherRouter, thisIP, otherIP, thisMask, otherMask, distance);
    }

    public void removeLink(int idRouter) {
        c.removeLink(idRouter);
    }

    public void setMatrix(ArrayList<Integer> ids) {
        c.setMatrix(ids);
    }

    public String showMatrix() {
        return c.showMatrix();
    }

    public int getMask() {
        return this.mask;
    }

    public String getLinks() {

        return c.getLinks();

    }
    
    
    

    public void setMatrixNewValues(String v[]) {

        int id = Integer.parseInt(v[0]);
        int pos = Integer.parseInt(v[1]);

        for (int i = 0; i < c.matrixSize; i++) {
            c.matrix[pos][i] = Integer.parseInt(v[2 + i]);
        }

        int x, y;
        int l = pos;

        for (int j = 0; j < c.matrixSize; j++) {
            for (int n = 0; n < c.matrixSize; n++) {
                if (l != n) {
                    x = c.matrix[l][j];
                    if (c.matrix[n][j] < x) {
                        y = c.matrix[n][j] + c.matrix[l][n];
                        if (y < x) {
                            System.out.println("troca");
                            c.matrix[l][j] = y;
                        }
                    }
                }
            }
        }

    }

}
